#!/usr/bin/perl

#
# generates a user-friendly experimental parameter file from a .itc file
#

open(ITCFILE, $ARGV[0]) || die "Can't open $ARGV[0]: $!\n";
@itc = <ITCFILE>;
close(ITCFILE);

$mode = 0;
@injections = ();
@sample = ();
@misc = ();

foreach $l(@itc)
{
	# $mode:
	# 0 = not to inj. info yet
	# 1 = reading inj. info
	# 2 = reading sample info
	# 3 = reading misc. info
	# 4 = done
	
	# '$Boolean' marks start of inj. info
	if (($l =~ /^\$False,/) || ($l =~ /^\$True,/)){
		$mode = 1;
	}
	
	# '#' indicates start of sample info
	if ($l =~ /^\#/){
		$mode = 2;
	}
	
	# '?' indicates start of misc. info
	if ($l =~ /^\?/){
		$mode = 3;
	}
	
	# '@' indicates start of exp. data
	if ($l =~ /^\@/){
		$mode = 4;
	}
	
	# format is $ 1 , 2 , 180 , 3
	# e.g. $ volume , duration , spacing , filter
	if (($mode == 1) && ($l =~ /^\$ (\d+\.?\d*) , (\d+\.?\d*) , (\d+) , (\d+)/)){
		push (@injections,"\t$1\t$2\t$3\t$4\n");
	}
	
	if (($mode == 2) && ($l =~ /^\# (\S*)/)){
		push (@sample,$1);
	}
	
	if (($mode == 3) && ($l =~ /^\% (\S*)/)){
		push (@misc,$1);
	}
}

print "ITC Experimental Parameters\n";
print "Generated by itcparse, (c) 2010 E. Ihms\n";
print "\n";
print "Experimental file:\t$ARGV[0]\n";
print "\n";

print "Experimental Parameters\n";
print "\tNumber of injections:\t". ($itc[1] =~ /^\$\s*(\d+)\s*/)[0] . "\n";
print "\tReference power:\t". ($itc[6] =~ /^\$\s*(\d+\.?\d*)\s*/)[0] . "\tuCal/sec.\n";
print "\tCell temperature:\t". ($itc[3] =~ /^\$\s*(\d+\.?\d*)\s*/)[0] . "\tdeg. C\n";
print "\tInitial delay:\t". ($itc[4] =~ /^\$\s*(\d+)\s*/)[0] . "\tsec.\n";
print "\tSyringe concentration:\t".$sample[1]."\tmM\n";
print "\tCell concentration:\t".$sample[2]."\tmM\n";
print "\tStirring speed:\t". ($itc[5] =~ /^\$\s*(\d+)\s*/)[0] . "\trpm\n";
# what is line 8?
print "\n";

$itc[8] =~ /^\$ADCGainCode\:\s+(\d+)/;
if ($1 eq '0'){
	print "Feedback Mode/Gain:\tHigh\n";
}else{
	print "Feeback Mode/Gain:\tUnknown\n";
}
print "\n";

$itc[9] =~ /^\$(\w+)\,(\w+),(\w+)/;
print "ITC Equilibration Options\n";
print "\tNo check temp.:\t$1\n";
print "\tFast equilibration:\t$2\n";
print "\tAutomatic:\t$3\n";
print "\n";

print "Injections:\n";
print "\tVolume\tDuration\tSpacing\tFilter\n";
print "\tuL\tsec.\tsec.\tsec.\n";
foreach $s(@injections){
	print $s;
}
